package config;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * @author HaoChen
 */
public class ConfigManager  {
	 /**
	 * 
	 */
	 private static ConfigManager theConfigurator=null; 
	 private String configFileName=System.getProperty("user.dir")+"/content//config.xml";
	 private Document configdoc=null;
	 private Configuration config=null;
	
	/**
	 * 
	 */
	private ConfigManager(){
		super();
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder parser=factory.newDocumentBuilder();
			configdoc=parser.parse(configFileName);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//TO-DO read the "cofiguration.xml" file and init the arguments
	}
	/**
	 * @return
	 */
	public static ConfigManager Instance(){
		if(theConfigurator==null){
			theConfigurator=new ConfigManager();
			theConfigurator.updateConfig();
		}
		return theConfigurator;
	};
	
	/**
	 *   è¯»å�–é…�ç½®æ–‡ä»¶,è¿”å›žé…�ç½®ä¿¡æ�¯ç±»Configurationå®žä¾‹
	 * @return
	 */
	public Configuration updateConfig(){
			config=new Configuration();
			//Read information from config file
			NodeList configs=configdoc.getElementsByTagName("Configuration");
			Node configNode=configs.item(0);
			NodeList configInfor=configNode.getChildNodes();
			
			String server_ip=null;
			int server_port=3306;
			String dbname=null;
			String table_name=null;
			String username=null;
			String psw=null;
			String xlsfilename=null;
			
			for(int j=0;j<configInfor.getLength();j++){
				if(configInfor.item(j).getNodeName().equalsIgnoreCase("server_ip")){
					server_ip=configInfor.item(j).getTextContent();	
					config.setServer_ip(server_ip);
				}else if(configInfor.item(j).getNodeName().equalsIgnoreCase("server_port")){
					server_port=Integer.parseInt(configInfor.item(j).getTextContent());
					config.setServer_port(server_port);
				}else if(configInfor.item(j).getNodeName().equalsIgnoreCase("dbname")){
					dbname=configInfor.item(j).getTextContent();
					config.setDbname(dbname);
				}else if(configInfor.item(j).getNodeName().equalsIgnoreCase("table_name")){
					table_name=configInfor.item(j).getTextContent();
					config.setTable_name(table_name);
				}else if(configInfor.item(j).getNodeName().equalsIgnoreCase("username")){
					username=configInfor.item(j).getTextContent();
					config.setUsername(username);
				}else if(configInfor.item(j).getNodeName().equalsIgnoreCase("password")){
					psw=configInfor.item(j).getTextContent();
					config.setPsw(psw);
				}else if(configInfor.item(j).getNodeName().equalsIgnoreCase("xlsfilename")){
					xlsfilename=configInfor.item(j).getTextContent();
					config.setXlsfilename(xlsfilename);
				}
			}
			System.out.println("'"+server_ip+" "+server_port+" "+dbname+" "+table_name+" "+username+" "+psw+" "+xlsfilename);
		
			return config;
	}
	/**
	 * 
	 * @param _config è®¾ç½®é…�ç½®ä¿¡æ�¯,å¹¶å†™å…¥é…�ç½®æ–‡ä»¶
	 */
	public void setConfig(Configuration _config){
		config=_config;
		//Write Configuration into config.xml
		
	}
	/**
	 * @return è¿”å›žé…�ç½®å½“å‰�ä¿¡æ�¯ç±»Configurationå®žä¾‹
	 */
	public Configuration getConfig(){
		return config;
		
	}
	
}

